package com.ihtry.config;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;

public class MyBatisConfig {
    /**
     * 通过SqlSessionFactoryBean对象获取session
     *
     * @param dataSource
     * @return org.mybatis.spring.SqlSessionFactoryBean
     */
    @Bean
    public SqlSessionFactoryBean sessionFactoryBean(DataSource dataSource) {
        //创建对象
        SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
        //设置数据源
        sessionFactoryBean.setDataSource(dataSource);
        //设置映射实体对象
        sessionFactoryBean.setTypeAliasesPackage("com.ihtry.pojo");
        return sessionFactoryBean;
    }

    /**
     * 映射dao接口
     *
     * @return org.mybatis.spring.mapper.MapperScannerConfigurer
     */
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer msc = new MapperScannerConfigurer();
        //设置dao接口包
        msc.setBasePackage("com.ihtry.dao");
        return msc;
    }

}
